Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    content: '',
  },

  /**
   * 输入标题时触发
   */
  onTitleInput: function(e) {
    this.setData({
      title: e.detail.value
    });
  },

  /**
   * 输入内容时触发
   */
  onContentInput: function(e) {
    this.setData({
      content: e.detail.value
    });
  },

  /**
   * 保存笔记
   */
  saveNote: function() {
    const that = this;
    if (!this.data.title.trim()) {
      wx.showToast({
        title: '标题不能为空',
        icon: 'none'
      })
      return;
    }
    const note = {
      id: Date.now().toString(), // 用时间戳作为ID
      title: that.data.title,
      content: that.data.content
    };
    const notes = wx.getStorageSync('notes') || [];
    notes.push(note);
    wx.setStorageSync('notes', notes);
    wx.navigateBack({
      delta: 1
    });
  }
});